package com.heu.blood.dict.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heu.blood.common.utils.PageUtils;
import com.heu.blood.common.utils.Query;
import com.heu.blood.dict.dao.DictTypeDao;
import com.heu.blood.dict.entity.DictDataEntity;
import com.heu.blood.dict.entity.DictTypeEntity;
import com.heu.blood.dict.service.DictDataService;
import com.heu.blood.dict.service.DictTypeService;
import com.heu.blood.dict.vo.DictTypeVo;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;


@Service("dictTypeService")
public class DictTypeServiceImpl extends ServiceImpl<DictTypeDao, DictTypeEntity> implements DictTypeService {

    @Autowired
    private DictTypeDao dictTypeDao;

    @Autowired
    private DictDataService dictDataService;

    /**
     * 分页条件查询字典管理数据项信息
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        // 构造DictTypeEntity对象去接收查询条件
        DictTypeEntity dictTypeEntity = new DictTypeEntity();
        // 添加查询条件
        if (Strings.isNotBlank((String) params.get("dictName"))){
            dictTypeEntity.setDictName((String) params.get("dictName")); //字典名称
        }
        if (Strings.isNotBlank((String) params.get("dictType"))){
            dictTypeEntity.setDictType((String) params.get("dictType")); // 字典类型
        }
        // 构造queryWrapper对象
        LambdaQueryWrapper<DictTypeEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件
        queryWrapper.eq(Strings.isNotBlank(dictTypeEntity.getDictName()), DictTypeEntity::getDictName, dictTypeEntity.getDictName())
                    .eq(Strings.isNotBlank(dictTypeEntity.getDictType()), DictTypeEntity::getDictType, dictTypeEntity.getDictType());
        IPage<DictTypeEntity> page = this.page(
                new Query<DictTypeEntity>().getPage(params),
                queryWrapper
        );
        // 返回结果
        return new PageUtils(page);
    }

    /**
     * 条件查询字典管理数据信息
     * @param dictTypeEntity
     * @return
     */
    @Override
    public List<DictTypeEntity> query(DictTypeEntity dictTypeEntity) {
        // 构造queryWrapper对象
        LambdaQueryWrapper<DictTypeEntity> queryWrapper = new LambdaQueryWrapper<>();
        // 添加查询条件
        queryWrapper.eq(Strings.isNotBlank(dictTypeEntity.getDictName()), DictTypeEntity::getDictName, dictTypeEntity.getDictName())
                .eq(Strings.isNotBlank(dictTypeEntity.getDictType()), DictTypeEntity::getDictType, dictTypeEntity.getDictType());
        // 返回结果
        return dictTypeDao.selectList(queryWrapper);
    }

    /**
     * 修改字典管理数据信息
     */
    @Override
    @Transactional
    public String updateDictById(DictTypeEntity dictTypeEntity) {
       //  如果要修改的内容里面包括dictType，则dictData里的dictType也要同步更改
        if (!Strings.isNotBlank(dictTypeEntity.getDictType())){
            // 否则直接修改就行
            this.updateById(dictTypeEntity);
            return "字典管理信息更新成功！";
        }
        // 先获得原本的dictType
        DictTypeEntity dictType = this.getById(dictTypeEntity.getId());
        String dictTypeOld = dictType.getDictType();
        // 先修改dictTypeEntity
        this.updateById(dictTypeEntity);
        // 再同步修改dictDataEntity
        List<DictDataEntity> dictDataEntities = dictDataService.selectDictData(dictTypeOld);
        dictDataEntities.forEach(dictDataEntity -> {
            dictDataEntity.setDictType(dictTypeEntity.getDictType()); // 更改为新的dictType
            dictDataService.updateById(dictDataEntity);
        });
        return "字典管理和字典数据信息更新成功！";
    }

    @Override
    public List<DictTypeVo> queryDictTypeList() {
        LambdaQueryWrapper<DictTypeEntity> queryWrapper = new LambdaQueryWrapper<>();
        List<DictTypeEntity> dictTypeEntities = dictTypeDao.selectList(queryWrapper);

        List<DictTypeVo> list = new ArrayList<>();
        for (DictTypeEntity entity: dictTypeEntities) {
            DictTypeVo dictTypeVo = new DictTypeVo();
            dictTypeVo.setId(entity.getId());
            dictTypeVo.setCode(entity.getDictType());
            dictTypeVo.setLabel(entity.getDictName());
            list.add(dictTypeVo);
        }

        return list;
    }
}